package com.wgl.express.service.Impl;

import com.wgl.express.entity.Package;
import com.wgl.express.mapper.PackageMapper;
import com.wgl.express.service.PackageService;
import com.wgl.express.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;

public class PackageServiceImpl implements PackageService {
    private int from = 0;
    private int count = 0;
    private int period = 10;
    private int status;

    public PackageServiceImpl(int status) {
        this.status = status;

    }

    @Override
    public List<Package> getPackByfromphone(String fromphone) {
        SqlSession sqlSession = null;
        List<Package> list = null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            PackageMapper mapper = sqlSession.getMapper(PackageMapper.class);
            list = mapper.selectByfromPhone(fromphone);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Package> getPackBytophone(String tophone) {
        SqlSession sqlSession = null;
        List<Package> list = null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            PackageMapper mapper = sqlSession.getMapper(PackageMapper.class);
            list = mapper.selectBytoPhone(tophone);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }


    @Override
    public List<Package> getPackByphone(String phone) {
        SqlSession sqlSession = null;
        List<Package> list = null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            PackageMapper mapper = sqlSession.getMapper(PackageMapper.class);
            list = mapper.selectByPhone(phone);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }


    @Override
    public int addPack(Package pack) {
        int s = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            PackageMapper mapper = sqlSession.getMapper(PackageMapper.class);
            s = mapper.insertPack(pack);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    @Override
    public int changePack(Package pack,boolean pickcode) {
        int s = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            PackageMapper mapper = sqlSession.getMapper(PackageMapper.class);
            if (pickcode) s = mapper.updatePickcode(pack);
            else s = mapper.updatePackWithoutPickcode(pack);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    @Override
    public List<Package> getPackByPid(int pid) {
        SqlSession sqlSession = null;
        List<Package> list = null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            PackageMapper mapper = sqlSession.getMapper(PackageMapper.class);
            list = mapper.selectByPid(pid);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Package> getPackUnbundled() {
        SqlSession sqlSession = null;
        List<Package> list = null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            PackageMapper mapper = sqlSession.getMapper(PackageMapper.class);
            list = mapper.selectByStatus(this.status, from, from + 11);
            from += 10;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    public int getFrameCount() {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            PackageMapper mapper = sqlSession.getMapper(PackageMapper.class);
            count = mapper.getCount(status);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (count % period == 0) ? (count / period) : (count / period) + 1;
    }

    @Override
    public int getFrameCountOfPost(int uid) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            PackageMapper mapper = sqlSession.getMapper(PackageMapper.class);
            count = mapper.getCountWithUid(status,uid);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (count % period == 0) ? (count / period) : (count / period) + 1;
    }

    @Override
    public List<Package> getPackStoraged(int uid) {
        SqlSession sqlSession = null;
        List<Package> list = null;
        try {
            sqlSession = MybatisUtils.getSqlSession();
            PackageMapper mapper = sqlSession.getMapper(PackageMapper.class);
            list = mapper.selectByStatusAndPost(this.status, from, from + 11, uid);
            from += 10;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
}
